How to resize row height based on the font of Grid cell content
15 Jun 20213 minutes to read
You can resize the row height based on the cell content height using ResizingRows event. Calculate the content height using MeasuringString() method. This considers the text, font style and font size to calculate the content size. Assign the calculated value to RowHeights property.
You can resize the rows individually using AllowResizingIndividualRows() method of GridHelperClasses. This method will be effective only when used before InitializeComponent().
public Form1()
{
//Used to Customizes the row height for individual row
GridEngineFactory.Factory = new Syncfusion.GridHelperClasses.AllowResizingIndividualRows();
InitializeComponent();
}
Size stringSize;
void TableControl_ResizingRows(object sender, GridResizingRowsEventArgs e)
{
//Resize the row on double click on the row header resizing cursor
if (e.Reason == GridResizeCellsReason.DoubleClick)
{
Graphics graphics = CreateGraphics();
long maxHeight = 0;
// Get the style info of the particular cell value
GridStyleInfo style = this.gridGroupingControl1.TableControl.GetViewStyleInfo(e.Rows.Bottom, 2);
stringSize = graphics.MeasureString(style.Text, style.GdipFont, this.gridGroupingControl1.TableModel.ColWidths[2]).ToSize();
if (maxHeight < stringSize.Height)
{
maxHeight = (long)stringSize.Height;
}
this.gridGroupingControl1.TableModel.RowHeights[e.Rows.Bottom] = (int)maxHeight;
e.Cancel = true;
}
}